package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemIconBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemIconDao;
import com.cw.jeeyt.dao.model.system.SystemIconDo;
import com.cw.jeeyt.service.converter.system.SystemIconConverter;
import com.cw.jeeyt.service.intf.system.SystemIconService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 图标配置表业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemIconServiceImpl implements SystemIconService {

    private static final Logger logger = LoggerFactory.getLogger(SystemIconServiceImpl.class);
    @Autowired
    private SystemIconDao systemIconDao;//图标配置表数据库操作接口

    /**
     * 查询所有图标配置表
     */
    @Override
    public List<SystemIconBo> querySystemIconList() throws Exception {
        return SystemIconConverter.doList2BoList(systemIconDao.querySystemIconList());
    }

    /**
     * 分页查询图标配置表
     *
     * @param systemIconBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemIconBo> queryByPage(SystemIconBo systemIconBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemIconDo> pageInfo = systemIconDao.queryByPage(SystemIconConverter.bo2Do(systemIconBo), startNum,
            pageSize);
        List<SystemIconBo> systemIconBos = SystemIconConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(systemIconBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("iconType", "icon_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(systemIconBos)), dictMap);
            systemIconBos = JSON.parseArray(jsonArray.toJSONString(), SystemIconBo.class);
        }
        return new PageInfo<>(pageInfo, systemIconBos);
    }

    /**
     * 根据主键查寻图标配置表信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemIconBo getSystemIconById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }
        return SystemIconConverter.do2Bo(systemIconDao.getById(id));
    }

    /**
     * 新增图标配置表
     *
     * @param systemIconBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SystemIconBo systemIconBo) throws Exception {

        if (systemIconBo == null) {
            logger.error("图标配置表为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemIconDo systemIcon = SystemIconConverter.bo2Do(systemIconBo);
        systemIcon.setCreateBy(userVo.getId());
        systemIcon.setCreateDate(new Date());
        systemIcon.setUpdateBy(userVo.getId());
        systemIcon.setUpdateDate(new Date());
        return systemIconDao.insert(systemIcon);
    }

    /**
     * 更新图标配置表信息
     *
     * @param systemIconBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SystemIconBo systemIconBo) throws Exception {

        if (systemIconBo == null) {
            logger.error("图标配置表为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemIconDo systemIcon = SystemIconConverter.bo2Do(systemIconBo);
        systemIcon.setUpdateBy(userVo.getId());
        systemIcon.setUpdateDate(new Date());

        systemIconDao.update(systemIcon);
    }

    /**
     * 删除图标配置表
     *
     * @param systemIconBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SystemIconBo systemIconBo) throws Exception {

        if (systemIconBo == null) {
            logger.error("图标配置表为空");
            return;
        }
        systemIconDao.delete(SystemIconConverter.bo2Do(systemIconBo));
    }

    @Override
    public List<SystemIconBo> loadIcon(SystemIconBo systemIconBo) throws Exception {
        return SystemIconConverter.doList2BoList(systemIconDao.loadIcon(SystemIconConverter.bo2Do(systemIconBo)));
    }

    /**
     * 批量新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batInsert(SystemIconBo systemIconBo) throws Exception {
        if (systemIconBo == null) {
            return;
        }
        String icons = systemIconBo.getIcons();
        SystemIconDo systemIcon = SystemIconConverter.bo2Do(systemIconBo);
        if (!StringUtils.isEmpty(icons)) {
            Long userId = SessionUtil.getSessionUserId();
            systemIcon.setCreateBy(userId);
            systemIcon.setCreateDate(new Date());
            systemIcon.setUpdateBy(userId);
            systemIcon.setUpdateDate(new Date());
            String[] iconList = icons.split(";");
            for (String icon : iconList) {
                systemIcon.setIconCode(icon);
                systemIcon.setIconName(icon);
                systemIconDao.insert(systemIcon);
            }
        }
    }
}
